#include "sComponentAlign.h"

namespace sgui
{
namespace res
{ 


sComponentAlign::sComponentAlign()
{
	clear();
}
	
void sComponentAlign::applyResize(math::sRectf& rect, math::sPointf sizeDifference) const
{
	float applyPointLeft = 0, applyPointRight = 0, applyPointTop = 0, applyPointBottom = 0;
	if (getAttachPoint(EAP_CENTER_HORISONTAL))
	{
		applyPointLeft += sizeDifference.X / 2;
		applyPointRight += sizeDifference.X / 2;
	}
	else
	{
		if (getAttachPoint(EAP_LEFT))
		{
			applyPointLeft += sizeDifference.X;
		}
		if (getAttachPoint(EAP_RIGHT))
		{
			applyPointRight += sizeDifference.X;
		}
	}
	if (getAttachPoint(EAP_CENTER_VERTICAL))
	{
		applyPointTop += sizeDifference.Y / 2;
		applyPointBottom += sizeDifference.Y / 2;
	}
	else
	{
		if (getAttachPoint(EAP_TOP))
		{
			applyPointTop += sizeDifference.Y;
		}
		if (getAttachPoint(EAP_BOTTOM))
		{
			applyPointBottom += sizeDifference.Y;
		}
	}

	rect.X += applyPointLeft;
	rect.W += applyPointRight - applyPointLeft;
	rect.Y += applyPointTop;
	rect.H += applyPointBottom - applyPointTop;
	
}

void sComponentAlign::save(db::Record& file) const
{
	file.setSize(EAP_COUNT);
	
	for (u32 i = 0; i < EAP_COUNT; ++i)
	{
		file[i].Set(getAttachPoint(i));
	}
}

void sComponentAlign::load(const db::Record& file)
{
	if (file.getSize() == EAP_COUNT)
	{
		for (u32 i = 0; i < EAP_COUNT; ++i)
		{
			setAttachPoint(i, file[i].GetAsBool());
		}
	}
}

void sComponentAlign::clear()
{
	for (u32 i = 0; i < EAP_COUNT; ++i)
	{
		setAttachPoint(i, false);
	}
}

#ifdef _EDITOR
void sComponentAlign::exportRTTI(rtti::sInfoList& list)
{

}
#endif

};
};